<template>
  <base-breadcrumb>
    <div class="about-master-container">
      <a-card v-loading="loading">
        <title-name title="基本信息"></title-name>
        <a-form-model
          :model="form"
          ref="formRules"
          :rules="formRules"
        >
          <a-row>
            <a-col span="8">
              <a-form-model-item
                label="培训主题"
                prop="trainTopic"
              >
                <b-input
                  v-model="form.trainTopic"
                  :disabled="disabled"
                />
              </a-form-model-item>
            </a-col>
            <a-col span="8">
              <a-form-model-item
                label="层级"
                prop="level"
              >
                <BSelect
                  disabled
                  v-model="form.level"
                  :list="options.level"
                ></BSelect>
              </a-form-model-item>
            </a-col>
            <a-col span="8">
              <a-form-model-item
                label="工会组织"
                prop="orgId"
              >
                <SelectTreeUnion
                  :disabled="disabled"
                  v-model="form.orgId"
                  @getOption="getOption"
                ></SelectTreeUnion>
              </a-form-model-item>
            </a-col>
            <a-col span="8">
              <a-form-model-item
                label="培训类型"
                prop="trainType"
              >
                <a-select
                  v-model="form.trainType"
                  :disabled="disabled"
                >
                  <a-select-option value="工会干部培训">工会干部培训</a-select-option>
                  <a-select-option value="职工素质培训">职工素质培训</a-select-option>
                  <a-select-option value="建筑产业工人素质技能培训">建筑产业工人素质技能培训</a-select-option>
                </a-select>
              </a-form-model-item>
            </a-col>
            <a-col span="8">
              <a-form-model-item
                label="主讲人"
                prop="speakerId"
              >
                <ASelectOrgPeople
                  :disabled="disabled"
                  @onSelectChange="form.speakerName = $event.name;"
                  @selectPeopleNameDone="form.speakerName = $event.name;"
                  :customRender="customRender"
                  :init="!form.id"
                  :selectPeopleName="form.speakerName"
                  v-model="form.speakerId"
                ></ASelectOrgPeople>
              </a-form-model-item>
            </a-col>
            <a-col span="8">
              <a-form-model-item
                label="培训时间"
                prop="trainTime"
              >
                <a-date-picker
                  v-model="form.trainTime"
                  valueFormat="x"
                  :disabled="disabled"
                />
              </a-form-model-item>
            </a-col>
            <a-col span="8">
              <a-form-model-item
                label="培训地点"
                prop="trainAddress"
              >
                <b-input
                  v-model="form.trainAddress"
                  :disabled="disabled"
                />
              </a-form-model-item>
            </a-col>
            <a-col span="24">
              <a-form-model-item
                label="培训记录"
                prop="trainLog"
              >
                <b-textarea
                  v-model="form.trainLog"
                  :disabled="disabled"
                />
              </a-form-model-item>
            </a-col>
            <a-col span="24">
              <a-form-model-item
                label="附件"
                prop="siteSize"
              >
                <b-upload
                  uploadType="single"
                  businessCode="UN02"
                  :fileList.sync="form.fileInfoVos"
                  :disabled="disabled"
                />
                <span v-if="!disabled">支持扩展名：.rar .zip .doc .docx .pdf .jpg...</span>
              </a-form-model-item>
            </a-col>
          </a-row>
        </a-form-model>
      </a-card>
      <div class="tar page-btn-right-top">
        <a-button
          v-if="!disabled"
          type="primary"
          :loading="loading"
          @click="save"
          class="mr10p"
        >提交</a-button>
        <a-button @click="$close">关闭</a-button>
      </div>
    </div>
  </base-breadcrumb>
</template>

<script>
import yiShiSanSaiApi from '@/api/union/yiShiSanSai'
import SelectOrgPeople from '@/components/Select/SelectOrgPeople.vue'
import staffEducation from '@/api/union/staffEducation'
import SelectTreeUnion from '@/components/Select/SelectTreeUnion'
import BSelect from '@/components/BComponents/BSelect'
import ASelectOrgPeople from '@/components/Select/ASelectOrgPeople'

export default {
  name: 'a' + Date.now(),
  components: {
    SelectOrgPeople,
    SelectTreeUnion,
    BSelect,
    ASelectOrgPeople
  },
  data() {
    return {
      disabled: this.$route.query.detail == 1,
      customRender: (q) => {
        let result = q.name
        if (q.postPNames && q.postPNames.length) {
          result = `${result} (${q.postPNames.join('/')}) `
        }
        return result
      },
      loading: false,
      form: {
        fileInfoVos: [],
        level: '',
        orgId: '',
        companyId: '',
        branchComId: '',
        projectId: '',
        speakerId: ''
      },
      formRules: {
        // level: { required: true, message: '层级不能为空', trigger: 'blur' },
        orgId: { required: true, message: '工会组织不能为空', trigger: 'change' },
        speakerId: { required: true, message: '主讲人不能为空', trigger: 'change' },
        trainAddress: { required: true, message: '培训地点不能为空', trigger: 'change' },
        trainTime: { required: true, message: '培训时间不能为空', trigger: 'change' },
        trainTopic: { required: true, message: '培训主题不能为空', trigger: 'change' },
        trainType: { required: true, message: '培训类型不能为空', trigger: 'change' },
        trainLog: { required: true, message: '培训记录不能为空', trigger: 'change' }
      },
      options: {
        level: []
      }
    }
  },

  created: async function () {
    if (this.$route.query.id) {
      this.form.id = this.$route.query.id
      await this.getBaseInfo()
    }
    this.getOption()
    this.getTypeLabel({ type: 1 })
  },
  methods: {
    getOption(e) {
      if (!e) return
      let levelId = e.unionLevelId // 层级id
      this.form.level = levelId
      this.form.orgName = e.unionOrgName
      /**
       * 添加表单组织机构参数
       */
      // 公司级
      if (levelId === '605004340528287745') {
        this.form.companyId = e.id
        // 分公司级
      } else if (levelId === '605004401341501446') {
        this.form.branchComId = e.id
        this.form.companyId = e.parentId
        // 项目级
      } else if (levelId === '605004528177254447') {
        this.form.projectId = e.id
        this.form.branchComId = e.parentId
      }
    },
    async getTypeLabel(args) {
      const { unionApiUrl } = require('@/api/config.js')
      const Union = require('@/api/union').default
      const { getLabels } = new Union(unionApiUrl, 'queryTypesLabelList')
      let res = await getLabels(args)
      this.options.level = res.data
    },
    onSelectChange(e, index) {
      this.form.speakerName = e.name
    },
    async getBaseInfo() {
      this.loading = true
      let res = await staffEducation.getWorkEducationById({}, { id: this.form.id })
      if (res.code === this.SUCCESS_CODE) {
        this.form = res.data
        this.form.fileInfoVos = this.form.fileInfoVos ? this.form.fileInfoVos : []
      }
      this.loading = false
    },
    async save() {
      // return this.$refs.t.open()
      this.$refs.formRules.validate(async (valid) => {
        if (valid) {
          this.loading = true
          let res
          if (this.form.id) {
            res = await staffEducation.updateByPrimaryKeySelective(this.form)
          } else {
            res = await staffEducation.insertSelective(this.form)
          }
          if (res.code === 200) {
            this.$message.success('保存成功')
            this.form.id = res.data.id
            await this.getBaseInfo()
            this.$close()
          }
          this.loading = false
        } else {
          this.$noticeWarning('请填写完整')
        }
      })
    }
  }
}
</script>
